#include "MainWindow.h"
#include "./ui_MainWindow.h"
#include "ui_RemoteCtlWidget.h"
#include "ui_Login.h"
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->name->hide();
    login = new Login(this);
    remoteCtlWidget = new RemoteCtlWidget(this);
    remoteCli = new RemoteClient(TCP | IPV4);
    this->initInerFace();
}


MainWindow::~MainWindow()
{
    delete remoteCtlWidget;
    delete remoteCli;
    delete login;
    delete ui;
}

void MainWindow::initInerFace()
{
    //登陆成功回调
    void(RemoteClient::*s1)(string) = &RemoteClient::noteLogSucess;
    void(MainWindow::*r1)(string) = &MainWindow::Logsucess;
    connect(remoteCli,s1,this,r1);

    //开启远程回调
    void(RemoteClient::*s2)(int) = &RemoteClient::noteRemoteCtlStatus;
    void(MainWindow::*r2)(int) = &MainWindow::RemoteCtl;
    connect(remoteCli,s2,this,r2);
}

int MainWindow::connectSrv(shared_ptr<ServerMsg::LoginMsg> LoginMsgPtr)
{
    remoteCli->initClient("127.0.0.1",8081);
    remoteCli->startClient(LoginMsgPtr);
}


void MainWindow::on_Load_clicked()
{
    //显示登陆界面
    login->show();
}

void MainWindow::on_Connect_clicked()
{
    remoteCli->reqRemoteCtl(ui->RemoteCodes->text().toStdString());
}

void MainWindow::Logsucess(string code)
{
    cout<<"登陆成功回调！"<<endl;
    ui->code->setText(QString::fromStdString(code));
    ui->Load->hide();
    ui->name->setText(u8"用户");
    ui->name->show();
    login->hide();
}

void MainWindow::RemoteCtl(int status)
{
    cout<<"开启远程回调！"<<endl;
    if(status == 0){
        QMessageBox::warning(this,"",u8"设备ID有误，或者目标id不在线");
        return;
    }else{
        //显示窗口
        remoteCtlWidget->show();
        //开始渲染
        remoteCtlWidget->ui->showFrame->beginUpdateFrame();
    }
}

